﻿/**
**		File : main.cpp
**		Author : beyondcode
**		Date : 2010/05/28
**		Purpose : 主执行程序,源代码
**
**/

#ifdef _WIN32
#include "vld.h"
#endif

#include "pak_kernel.h"
#include "debugheader.h"
#include "args.h"

#ifdef _UNICODE
#define TToU8 U2ToU8
#define U8ToT U8ToU2
#else
#define TToU8 AToU8
#define U8ToT U8ToA
#endif

int ShowHelp()
{
	printf( "beyondpak\n" );
	GetPlatformInfo();
	printf( "--f=packname:\tPackage file name to be pack or unpack\n" );
	printf( "--p=pathname:\tThe directory to be pack\n");
	printf( "--u=pathname:\tThe directory to be unpacked to\n");
	printf( "--l:\t\tList package file's all directory and file\n" );
	printf( "--v\t\tShow verbose information or not\n" );
	printf( "--h\t\tShow this information\n" );
	printf( "--version\tShow version information\n" );
	return 1;
}
int ShowVersion()
{
	GetPlatformInfo();
	printf( "beyondpack version 1.0\n" );
	return 1;
}

int OnEnumDirTree( PPAK_DIR_NODE pDirNode, void* pCallbackParam )
{
	char*		ptype = NULL;
	PPAK_DIR	pDir = NULL;

	PROCESS_ERROR( pDirNode );
	PROCESS_ERROR( pDir = (PPAK_DIR)pDirNode->psValue );
	if( pDir->nType == DIRTYPE_DIR )
		ptype = "D";
	else if ( pDir->nType == DIRTYPE_FILE )
		ptype = "F";

	printf( "[%s]\t%s\n", ptype, pDir->pszName );

Exit0:
	return 0;
}

int OnPreExtractFile( const char* pszFullName, const PPAK_DIR_NODE pCurNode, size_t nCurCount, size_t nTotalCount, void* pParam )
{
	PROCESS_ERROR( pCurNode && pszFullName );

	printf( "%s %d/%d\n", pszFullName, nCurCount, nTotalCount );
Exit0:
	return 1;
}

int OnExtractFile( const char* pszFullName, const PPAK_DIR_NODE pCurNode, size_t nCurByte, size_t nTotalByte, void* pParam )
{
	PROCESS_ERROR( pCurNode && pszFullName );

	printf( "%s %d/%d\n", pszFullName, nCurByte, nTotalByte );
Exit0:
	return 1;
}
int OnPostExtractFile( const char* pszFullName, const PPAK_DIR_NODE pCurNode, size_t nCurByte, size_t nTotalByte, void* pParam )
{
	PROCESS_ERROR( pCurNode && pszFullName );

	printf( "%s %d/%d\n", pszFullName, nCurByte, nTotalByte );
Exit0:
	return 1;
}

int OnPrePackFile( const char* pszFullName, const PPAK_DIR_NODE pCurNode, size_t nCurByte, size_t nTotalByte, void* pParam )
{
	PROCESS_ERROR( pCurNode && pszFullName );

	printf( "%s %d/%d\n", pszFullName, nCurByte, nTotalByte );
Exit0:
	return 1;
}
int OnPackFile( const char* pszFullName, const PPAK_DIR_NODE pCurNode, size_t nCurByte, size_t nTotalByte, void* pParam )
{
	PROCESS_ERROR( pCurNode && pszFullName );

	printf( "%s %d/%d\n", pszFullName, nCurByte, nTotalByte );
Exit0:
	return 1;
}
int OnPostPackFile( const char* pszFullName, const PPAK_DIR_NODE pCurNode, size_t nCurByte, size_t nTotalByte, void* pParam )
{
	PROCESS_ERROR( pCurNode && pszFullName );

	printf( "%s %d/%d\n", pszFullName, nCurByte, nTotalByte );
Exit0:
	return 1;
}


int _tmain( int argn, TCHAR** argv )
{
	int				bResult = -1;
	int				bShowHelp = 0;
	PKArgs			pArgs = NULL;

	int				bVersion = 0;
	int				bHelp = 0;
	int				bVerbose = 0;
	int				bListPackage = 0;
	const TCHAR*	ptszArgValue = NULL;
	char*			pszPackDirNameU8 = NULL;
	int				nPackDirNameLen = 0;
	char*			pszUnPackDirNameU8 = NULL;
	int				nUnpackDirNameLen = 0;
	char*			pszPackNameU8 = NULL;
	int				nPackNameLen = 0;
	PPAK_PKG		pPkg = NULL;
	if( argn < 2 )
	{
		//PrintUsage
		ShowHelp();
		PROCESS_ERROR( 0 );
	}
	PROCESS_ERROR( pArgs = GetArgs( argn, argv ) );

	//打包或解包的文件名
	ptszArgValue = GetStringItem( pArgs, _T("f") );
	if( ptszArgValue )
	{
		nPackNameLen = TToU8( ptszArgValue, -1, NULL, 0 );
		PROCESS_ERROR( pszPackNameU8 = (char*)malloc( sizeof(char) * nPackNameLen ) );
		TToU8( ptszArgValue, -1, pszPackNameU8, nPackNameLen );
	}
	//打包路径
	ptszArgValue = GetStringItem( pArgs, _T("p") );
	if( ptszArgValue )
	{
		nPackDirNameLen = TToU8( ptszArgValue, -1, NULL, 0 );
		PROCESS_ERROR( pszPackDirNameU8 = (char*)malloc( sizeof(char) * nPackDirNameLen ) );
		TToU8( ptszArgValue, -1, pszPackDirNameU8, nPackDirNameLen );
	}
	//解包路径
	ptszArgValue = GetStringItem( pArgs, _T("u") );
	if( ptszArgValue )
	{
		nUnpackDirNameLen = TToU8( ptszArgValue, -1, NULL , 0 );
		PROCESS_ERROR( pszUnPackDirNameU8 = (char*)malloc( sizeof(char) * nUnpackDirNameLen ) );
		TToU8( ptszArgValue, -1, pszUnPackDirNameU8, nUnpackDirNameLen );
	}
	bVersion = HasItem( pArgs, _T("version") );
	bVerbose = HasItem( pArgs, _T("v") );
	bHelp = HasItem( pArgs, _T("h") );
	bListPackage = HasItem( pArgs, _T("l") );
	PROCESS_ERROR( ReleaseArgs( pArgs ) );
	pArgs = NULL;
	
	if( !pszPackNameU8 )
		bShowHelp = 1;
	if( !pszPackDirNameU8 && !pszUnPackDirNameU8 )
		bShowHelp = 1;
	
	if( bHelp )
	{
		ShowHelp();
	}
	else if( bVersion )
	{
		ShowVersion();
	}
	else if( bListPackage )
	{
		PROCESS_ERROR( pPkg = LoadDirTreePackage( pszPackNameU8 ) );
		EnumDirTreePackage( pPkg, OnEnumDirTree, NULL );
	}
	else if( pszPackDirNameU8 )
	{
		
		PROCESS_ERROR( pPkg = CreateDirTreePackage( pszPackDirNameU8 ) );
		if( bVerbose )
		{
			//SetPackageCallback( pPkg, ON_PRE_PACKFILE, OnPrePackFile );
			//SetPackageCallback( pPkg, ON_PACKFILE, OnPackFile );
			//SetPackageCallback( pPkg, ON_POST_PACKFILE, OnPostPackFile );
		}
		PROCESS_ERROR( SaveDirTreePackage( pPkg, pszPackNameU8 ) );
	}
	else if( pszUnPackDirNameU8 )
	{
		PROCESS_ERROR( pPkg = LoadDirTreePackage( pszPackNameU8 ) );
		if( bVerbose )
		{
			//SetPackageCallback( pPkg, ON_PRE_EXTRACTFILE, OnPreExtractFile );
			//SetPackageCallback( pPkg, ON_EXTRACTFILE, OnExtractFile );
			//SetPackageCallback( pPkg, ON_POST_EXTRACTFILE, OnPostExtractFile );
		}
		PROCESS_ERROR( ExtractDirTreePackage( pPkg, pszUnPackDirNameU8 ) );
	}
	/*int nDirLenU8 = 0;
	int nNodeCount = 0;
	char* pDirU8 = 0;

	nDirLenU8 = TToU8( argv[1], -1, 0, 0 );
	pDirU8 = (char*)malloc( nDirLenU8 );
	TToU8( argv[1], -1, pDirU8, nDirLenU8 );

	PPAK_PKG pPkg = CreatePackage( pDirU8 );
	SetPackageCallback( pPkg, ON_PACKFILE, OnPostPackFile );
	SavePackage( pPkg, "newpackage.pkg" );
	DeletePackage( pPkg );*/

	//PPAK_PKG pPkg = LoadPackage( pDirU8 );
	//EnumDirTree( pPkg ? pPkg->pDirTree : NULL, OnEnumDirTree );
	//SetPreExtractFileCallback( pPkg, OnPreExtractFile );
	//SetExtractFileCallback( pPkg, OnExtractFile );
	//SetPostExtractFileCallback( pPkg, OnPostExtractFile );
	//SetPreExtractFileCallback( pPkg, OnPreExtractFile );
	//ExtractPackage( pPkg, "unpak" );
	//DeletePackage( pPkg );

	bResult = 0;
Exit0:
	if( bResult != 0 )
	{
		//PrintUsage
		if( bShowHelp )
		{
			ShowHelp();
		}
	}
	if( pszPackDirNameU8 )
		free( pszPackDirNameU8 );
	if( pszPackNameU8 )
		free( pszPackNameU8 );
	if( pszUnPackDirNameU8 )
		free( pszUnPackDirNameU8 );
	if( pArgs )
		ReleaseArgs( pArgs );
	if( pPkg )
		DeletePackage( pPkg );
	return bResult;
}
//Last line for linux

